<?php
require_once("../lib/fun.php");
require_once("../lib/pdo.php");

//vip数据
$db_vip = db('lu_vip');
$_vip = $db_vip->select();

function xmltoarray($data){
    $obj = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
    $json = json_encode($obj);
    $arr = json_decode($json, true);      
    return $arr;

}

$param = file_get_contents("php://input");

$info_arr = xmltoarray($param);
file_put_contents("./wx.txt",json_encode($info_arr)."<---------------------------------------------------------------------->",FILE_APPEND);
if($info_arr){
    $db = db('lu_order');
    $where = [
            'order_sn' => $info_arr['out_trade_no'],
        ];
    $order_info = $db->where($where)->find();
    //成功
    if($info_arr['result_code'] == 'SUCCESS'){
        if($order_info['status'] == 1){
            $vip_id = $order_info['vip_id'];
            $vip_data = returnArrByKey($_vip,'id');
            $vip_time = $vip_data[$vip_id]['month'] * 30 * 24 * 3600;
            
            $vip_info = $vip_data[$vip_id];
            //更新账单信息
            $db->where($where)->where($where)->update([
                    'status' => 2,
                ]);
            //更新用户信息
            $where = [
                'id' => $order_info['uid'],
                ];
            $db_user = db("lu_user");
            $user_info = $db_user->where($where)->find();
            
            //普通会员
            if($vip_info['type'] == 0){
                if($user_info['vip_time'] < time()){
                    $update = [
                        'vip_time' => time() + $vip_time,
                        'vip_id' => $order_info['vip_id'],
                        'limit_plan_num' => $vip_info['plan_num'],
                        'limit_address_num' => $vip_info['address_num'],
                        'type' => $vip_info['type'] ?? 0,
                        
                        ];
                    $res = $db_user->where($where)->update($update);
                }else{
                    $update = [
                        'vip_time' => $user_info['vip_time'] + $vip_time,
                        'vip_id' => $order_info['vip_id'],
                        'limit_plan_num' => $vip_info['plan_num'],
                        'limit_address_num' => $vip_info['address_num'],
                        'type' => $vip_info['type'] ?? 0,
    
                        ];
                    $res = $db_user->where($where)->update($update);
                }
            }else{
                if($user_info['business_time'] < time()){
                    $update = [
                        'business_time' => time() + $vip_time,
                        'vip_id' => $order_info['vip_id'],
                        'limit_plan_num' => $vip_info['plan_num'],
                        'limit_address_num' => $vip_info['address_num'],
                        'business_user_limit' => $vip_info['business_user_limit'],
                        'business_share_limit' => $vip_info['business_share_limit'],
                        'type' => $vip_info['type'] ?? 0,
                        
                        ];
                    $res = $db_user->where($where)->update($update);
                }else{
                    $update = [
                        'business_time' => $user_info['business_time'] + $vip_time,
                        'vip_id' => $order_info['vip_id'],
                        'limit_plan_num' => $vip_info['plan_num'],
                        'limit_address_num' => $vip_info['address_num'],
                        'business_user_limit' => $vip_info['business_user_limit'],
                        'business_share_limit' => $vip_info['business_share_limit'],
                        'type' => $vip_info['type'] ?? 0,
    
                        ];
                    $res = $db_user->where($where)->update($update);
                }
            }
            file_put_contents("./wx.txt","<-------->".$res,FILE_APPEND);
            
        }
        
        
        
    }
}


?>